﻿using System;
using System.Windows;

namespace DotNetImage.Imaging.Filters
{
    public class RandomJitter : FilterBase
    {
        private short nHalf = 0;
        private Point[,] points = null;
        private Random rnd = new Random();

        [ParameterAttribute(Name = "Degree", DefaultValue = 0, MaxValue=short.MaxValue, MinValue=short.MinValue, Step=1)]
        public short Degree { get; set; }


        protected override void PrepareData(DotNetImage bmp)
        {
            bmp.CreatePixelCopy();
            points = new Point[nWidth, nHeight];
            nHalf = (short)Math.Floor((decimal)Degree / 2);
        }

        protected override void ProcessPixel(ref Pixel pix, uint x, uint y)
        {
            int newX = rnd.Next(Degree) - nHalf;
            points[x, y].X = (x + newX > 0 && x + newX < nWidth) ? newX : 0;

            int newY = rnd.Next(Degree) - nHalf;
            points[x, y].Y = (y + newY > 0 && y + newY < nHeight) ? newY : 0;
        }

        protected override void PostProcessing(DotNetImage bmp)
        {
            base.PostProcessing(bmp);
            OffsetFilter filter = new OffsetFilter();
            filter.Offset = points;
            filter.Apply(bmp);
        }
    }
}
